﻿var prop_is_help = false;  //是否帮忙
var prop_deng_list = '';
var prop_tip_list = '';//提示信息




var prop_is_chou = true;
var prop_buy_url = '';
var prop_help_count = 0;
var prop_no_uploadbg = false;
var pub_id = ''

function func_page_init__success(data, textStatus, jqXHR)
{
	prop_is_help = data.page.is_help;
	prop_deng_list = data.page.like_list;
	prop_is_chou = data.page.is_chou;//是否抽过了
	prop_buy_url = data.common.entity_abc.abc_url;//购票链接
	prop_help_count =  data.page.help_count;
	pub_id = data.page.id;

	var type = data.common.entity_abc.abc_type;

	var hd_img = data.common.entity_wxuser.wxuser_headimgurl;
	var banner = data.common.entity_abc.abc_pic_top;	
	var prize_type = data.common.entity_abc.abc_type;//类型
	
	//是否已完成
	var is_done = data.page.is_done;


	$(".hd_img").attr('src',func_get_wx_headimgurl(hd_img,64));
	$(".banner").attr('src',img_url(banner));
	
	if(data.common.entity_abc.abc_file_00=='' ){
		prop_no_uploadbg = true;
	}else{
		prop_no_uploadbg = false;
	}
	
	//活动说明
	var rule = data.common.entity_abcact.abcact_desc.split('\n');
	for (var a= 0; a < rule.length;a++) {
		var html = '<li>'+rule[a]+'</li>';
		$(".rule_content").find('ol').append(html);
	}

	
	for (var i = 0; i < prop_deng_list.length; i++) {
		var item =  prop_deng_list[i];
		var img = item.pic;
		if( item.open ){
			var html = '<div class="deng_item flex active"><img src="'+img_url(img)+'"/></div>'
		}else{
			var html = '<div class="deng_item flex gray"><img src="'+img_url(img)+'"/></div>'
		}

		$(".deng_area").append(html);
	}
	setItem();



	if(is_done)
	{
		$(".top_tips").html('赶紧求助小伙伴帮忙点开亮区域吧~');
		$(".bottom_tips").html("区域已全部点亮");

		
		if(type == "chou")
		{
			//抽奖模式
			$(".btn_success").html('马上<br>抽奖').show().click(function(){
				if(prop_is_chou){
					$('#toast').toast('你已经抽过了哦~');
				}else{
					func_chou();
				}
			});

			$(".text_tip").html('点击上方按钮马上抽奖~')

			if(prop_is_chou)
			{
				var pic='';
				if(data.page.is_chou__entity_actprize != null)
				{
					pic = img_url(data.page.is_chou__entity_actprize.actprize_show_pic_s);
				}
				else
				{
					pic = "/x_res/admin_app/act_add/act_add-0001/img/noprise_img_1.png";
				}
				$(".page_modal_chou").show();
				$(".p_pic").attr('src',pic);
				$(".btn_chou_use").click(function(){
					window.location = 'a__word_re.aspx?getWord=prize';
				})
			}
		}
		else
		{
			//普通模式
			$(".btn_success").html('立刻<br>前往').show().click(function(){
				if(prop_buy_url)
				{
					window.location = prop_buy_url;
				}
			});
			$(".text_tip").html('点击上方按钮有惊喜哦~');
		}
	}
	else
	{
		$(".top_tips").html('赶紧求助小伙伴帮忙点开亮区域吧~');
		$(".bottom_tips").html("已点亮{#help_count}个区域，继续加油".replace("{#help_count}",prop_help_count));
	}
	
	open_id = data.common.entity_wxuser.wxuser_openid;
	prop_name = data.common.entity_wxuser.wxuser_nickName;
	
	
	var percent = prop_help_count/prop_deng_list.length*100;
	progress(percent);
}

       
function func_help__success(data, textStatus, jqXHR)
	{
		if(data.errcode == 0)
		{
			$('#toast').toast('你已点亮成功！');
			prop_is_help = true;
			
			var percent = (prop_help_count+1)/prop_deng_list.length*100;
			progress(percent);
			
			rotateEnd();
			$(".deng_item").eq(prop_help_count).removeClass('gray').addClass('bounceIn');

			$(".bottom_tips").html("已点亮{#help_count}个区域，继续加油".replace("{#help_count}",prop_help_count+1));
		}
		else
		{
			func_alert(data.errmsg);
		}
	}


function func_chou__success(data, textStatus, jqXHR) {
	console.log(data);
	console.log(textStatus);
	console.log(jqXHR);

	if (data.errcode == 0) {
		var pic = '';
		if(data.prize.entity_actprize!=null){
			pic= img_url(data.prize.entity_actprize.actprize_show_pic_s);
		}else{
			pic= "/x_res/admin_app/act_add/act_add-0001/img/noprise_img_1.png";
		}
		$(".page_modal_chou").show();
		$(".p_pic").attr('src',pic);
		$(".btn_chou_use").click(function(){
			window.location = 'a__word_re.aspx?getWord=prize';
		})
		
	}else {
		$('#toast').toast(data.errmsg);
	}
}



/*
 * 分享海报
 */
var can = null,pen = null;
var total_sca = window.innerWidth/320,
	head_w = 130*total_sca,head_h = 130*total_sca,
	qr_w = 190*total_sca,qr_h = 190*total_sca;
var prop_name = '',p_url = '',prop_get_state = false,open_id = '',
	prop_poster_bg = 'img/poster.jpg';
$(function(){
	can = document.getElementById('canvas');
	pen = can.getContext('2d');
	can.width = 640*total_sca;
	can.height = 1136*total_sca;
	
	$(".btn_share").click(function(){
		if( prop_no_uploadbg ){//没传底图
			func_show_share_mask();
		}else{
			if(	!prop_get_state ){
				func_help_qr();
	
			}else{
				$(".page_poster").show();
				$(".create_p").show();
			}
		}
		
		
	})
	
	$(".poster").click(function(e){
		e.stopPropagation();
	});
	$(".page_poster").click(function(){
		$(".page_poster").hide();
	})
	
})

//获取二维码
function func_help_qr()
{
	var url = 'api__api.aspx';

	$.ajax({
		dataType: "json",
		url: url,
		data:
		{
			__type: "func_help_qr",
			actlog_id: prop_entity_actlog_y__master.actlog_id,
			__d: (new Date()).valueOf()
		},
		//////////////////////////////////////////////////
		success: function (data, textStatus, jqXHR)
		{
			setTimeout(function ()
			{
				func_help_qr__success(data, textStatus, jqXHR);
			}, 100);
		},
		//////////////////////////////////////////////////
		error: function (data, textStatus, jqXHR)
		{
		}
	});
}
function func_help_qr__success(data, textStatus, jqXHR)
{
	if (data.errcode == 0)
	{
		func_mask(true);
		prop_url = data.qr_url;
		drawPoster(prop_poster_bg,prop_name,prop_url);
	}
	else
	{
		alert(data.errmsg);
	}
}
function drawPoster(p_bg,p_name,p_url){
//	console.log(p_id+'id');
	var poster_bg = new Image();
		poster_bg.src = 'api__api.aspx?__type=func_get_haibao_bg_url';
	
		poster_bg.onload = function(){
			pen.drawImage(poster_bg,0,0,can.width,can.height);
			console.log('绘制底图完成')
			
			
			var headUrl = new Image();
			headUrl.src = "/z_web/wx_headimg.aspx?wx_from="+open_id;
//			headUrl.src = prop_hb_headimgurl;
			headUrl.onload=function(){
				
				var can2 = document.createElement('canvas');
					pen2 = can2.getContext('2d');
					can2.width = can.width;
					can2.height = can.height;
				pen2.drawImage(headUrl,110*total_sca-head_w/2,935*total_sca,head_w,head_h);
				console.log('绘制头像完成');
				
				// 创建图片纹理
			    var pattern = pen.createPattern(can2, "repeat");
			    // 如果要绘制一个圆，使用下面代码
			    pen.arc(110*total_sca, 935*total_sca+head_w/2, Math.max(head_w, head_h) / 2, 0, 2 * Math.PI);
   			    // 填充绘制的圆o
			    pen.fillStyle = pattern;
			    pen.fill();    
			    
			    pen.arc(110*total_sca, 935*total_sca+head_w/2, Math.max(head_w, head_h) / 2,0,2*Math.PI);
			    pen.strokeStyle = '#fff';
			    pen.lineWidth = 4;
				pen.stroke();
				
				var qrUrl = new Image();
				qrUrl.src = p_url;
				qrUrl.onload=function(){
					pen.drawImage(qrUrl,223*total_sca,898*total_sca,qr_w,qr_h);
//					console.log('绘制二维码完成');
					
					pen.font = 22*total_sca+"px '微软雅黑'";
					
					console.log("文本"+pen.measureText(p_name).width)
				    //设置字体填充颜色
				    pen.fillStyle = "#333";
				    //从坐标点(50,50)开始绘制文字
				    pen.textAlign="center";
				    
				    if (p_name.length>6) {
						p_name = p_name.substring(0,6);
						p_name = p_name+'...';
						console.log(p_name);
					}
				    pen.fillText(p_name, 110*total_sca, 1093*total_sca,116);
				    
					var result = can.toDataURL("image/jpeg");
					console.log('生成海报');
					$(".create_p").remove();
					$(".poster_tip").show();
					$(".poster").attr('src',result);
					
					prop_get_state = true;
					func_mask(false);
					$(".page_poster").show();
					$(".create_p").show();
				}
				
			}
		}
}